package it.softphone.cmc.server.rest;

import it.softphone.cmc.server.service.ScriptletManagerService;
import it.softphone.cmc.server.service.exception.CMCException;
import it.softphone.cmc.shared.entity.AssetImpl;
import it.softphone.cmc.shared.entity.BaseResultSet;
import it.softphone.cmc.shared.entity.ResultSet;
import it.softphone.cmc.shared.entity.ScriptletAsset;
import it.softphone.cmc.shared.security.Permissions;
import it.softphone.commons.json.server.JSONUtils;
import it.softphone.commons.json.server.jersey.JSONParam;
import it.softphone.commons.spring.ApplicationConfig;

import java.util.Collections;

import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 
 * @author softphone
 *
 */
@Path("/scriptlet")
public class CMCScriptletAssetManager  extends RESTServiceAware {

	public static class  ScriptletResultSet extends BaseResultSet<String> implements ResultSet<String> {
	}

	protected ScriptletManagerService script() {

		ScriptletManagerService service = ApplicationConfig.getBean(ScriptletManagerService.class);
		
		return service;
		
	}

	@POST 
	@Consumes( MediaType.APPLICATION_FORM_URLENCODED)	
	@Produces(MediaType.TEXT_PLAIN)
	@RequiresPermissions( Permissions.Script.SAVE )
    public Response saveScriptlet( @FormParam("asset") JSONParam param ) throws CMCException
	{

    	logger.info( "saveScriptlet " + param );

    	try {
			
    		final ScriptletAsset s = script().makeScriptletFromJson(param.toObject());

    		script().saveScriptlet(s);
						
			return Response.ok("OK").build();
		
		} catch (Exception e) {
			
			throw new CMCRestException(Response.Status.BAD_REQUEST, e);
			
		}
    }
	
 	@GET 
	@Produces(MediaType.APPLICATION_JSON)
	//@RequiresPermissions( Permissions.Script.SELECT )
    public it.softphone.commons.json.server.JSONObject getScriptlet( @QueryParam("key") final String key ) throws CMCException {

    	logger.info( String.format("get Scriptlet [%s]", key));

    	it.softphone.commons.json.server.JSONObject result = null;

    	ScriptletAsset s = script().findScriptletByKey( key );
		
		if( s!=null ) {
		
			result =  new it.softphone.commons.json.server.JSONObject( s );	

		}
		else {
			final String msg = String.format("getScriptlet: Scriptlet [%s] not found!", key);
			
			throw new CMCRestException( Response.Status.NOT_FOUND, msg );
			
		}

		return result;
    }
 
 	@GET 
	@Produces(MediaType.APPLICATION_JSON)
	@Path("keys")	
	//@RequiresPermissions( Permissions.Script.SELECT )
    public it.softphone.commons.json.server.JSONObject getAllKeys( @QueryParam("asset") @DefaultValue("{}") JSONParam asset ) throws CMCException 
 	{

    	logger.info( String.format("get Scriptlet assets [%s] ", asset ));

    	
    	try {
        	AssetImpl filter = new AssetImpl();
        	
        	JSONUtils.copyTo(filter, asset.toObject());

    		java.util.List<String> keyList = script().findScriptletKeys(filter);
    			    	
			ScriptletResultSet result = new ScriptletResultSet();
			
			if( keyList==null ) {
				keyList = Collections.emptyList();
			}
			
			result.setResult( keyList);
	
			return new it.softphone.commons.json.server.JSONObject( result );

    	}
    	catch( Exception e ) {
    	
			throw new CMCRestException(Response.Status.INTERNAL_SERVER_ERROR, e);
    	}
    }
 	
}
